import React, {useState} from "react";
import {defaultPagination, paginationTool} from "@/utils/util";
import {useMount, useRequest} from "ahooks";
import ProTable from "@ant-design/pro-table";
import {getResult} from "@/services/ant-design-pro/attribute";
import {findByCouponPageList} from "@/services/ant-design-pro/coupon";

const columns = [
  {
    title: '编号',
    dataIndex: 'id',
    search: false,
  },

  {
    title: '名称',
    dataIndex: 'name',
    search: false,
  },
  {
    title: '优惠授权',
    dataIndex: 'couponCode',
    search: false,
  },
  {
    title: '金额/积分',
    dataIndex: 'price',
    search: false,
    render: ((_, row, index, action) => {
      return row.type == 0 ? row.integral + '' : row.price + '￥'
    })
  },
  {
    title: '类型',
    dataIndex: 'type',
    search: false,
    render: ((_, row, index, action) => {
      return row.type == 0 ? '积分' : '领钱'
    })
  },
  {
    title: '开始时间',
    dataIndex: 'startDate',
    valueType: 'dateTime',
    hideInSearch: true,
  }, {
    title: '结束时间',
    dataIndex: 'endDate',
    valueType: 'dateTime',
    hideInSearch: true,
  },
  {
    title: '时间',
    dataIndex: 'createDate',
    valueType: 'dateTime',
    hideInSearch: true,
  },
  {
    title: '状态',
    dataIndex: 'state',
    valueType: 'select',
    valueEnum: await getResult ( 'backsatgeloginstate' ),
  },
];
const Page: React.FC = () => {

//findByCouponPageList
  const [pagination, setPagination] = useState<API.Pagination> ( defaultPagination () );
  const {
    run: fetchByPageList,
    refresh,
    data: dataList,
    loading,
  } = useRequest ( findByCouponPageList, {
    debounceWait: 100,
    manual: true,
  } );

  useMount ( () => {
    fetchByPageList ();
  } );

  return (
    <ProTable
      onDataSourceChange={(d) => {
        console.log ( 'onDataSourceChange', d );
      }}
      onLoad={(d) => {
        // 数据加载完操作
        console.log ( 'onDataSourceChange', d );
      }}
      dataSource={dataList?.data?.list}
      loading={loading}
      pagination={paginationTool ( pagination, dataList?.data?.total, (data1: API.Pagination) => {
        setPagination ( data1 );

        fetchByPageList ( {...pagination} );
      } )}
      search={false}
      cardBordered={true}
      columns={columns}
    ></ProTable>
  );
}

export default Page;
